package com.sfzd5.UI;

import com.sfzd5.aiclassroom.modle.Question;
import com.sfzd5.exam.helplibrary.Generator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class QuestionSelectPanel {

    private static ButtonGroup cbGroup = new ButtonGroup();

    public JRadioButton cb;
    public JPanel p;
    private JTextPane htmlPane;
    public Question q;

    public boolean hasSelected() {
        return cb.isSelected();
    }

    QuestionSelected questionSelected;

    public QuestionSelectPanel(Question q, QuestionSelected questionSelected) {

        cbGroup.add(cb);

        this.q = q;
        this.questionSelected = questionSelected;
        cb.addItemListener(itemListener);
        htmlPane.setText(Generator.generateQuestionHtml_forPanel(q));

        htmlPane.setMaximumSize(new Dimension(1000, 1000));
        htmlPane.setMinimumSize(new Dimension(1000, 160));
        //htmlPane.setPreferredSize(new Dimension(1000, 0));
    }

    ItemListener itemListener = new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            if (questionSelected != null)
                questionSelected.stateChange(cb.isSelected(), q);
        }
    };

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        p = new JPanel();
        p.setLayout(new BorderLayout(0, 0));
        p.setMaximumSize(new Dimension(1000, 1000));
        p.setMinimumSize(new Dimension(1000, 100));
        htmlPane = new JTextPane();
        htmlPane.setContentType("text/html");
        htmlPane.setMaximumSize(new Dimension(950, 1000));
        htmlPane.setMinimumSize(new Dimension(950, 100));
        p.add(htmlPane, BorderLayout.CENTER);
        cb = new JRadioButton();
        cb.setText("");
        p.add(cb, BorderLayout.WEST);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return p;
    }
}
